/*
  Copy this file to create basic service with list, get, patch, create functions
  Replace the following strings:
  `ExampleInterface` -> `YourModelInterFace` (the interface)
  `path` in url function
*/
import { api } from '../boot/axios'
import { EventLogInterface, LogOpts, MaxPage } from './type'

const log_type = {
  PART : 'part master',
  STOCK : 'stock master',
  MAINTENANCE : 'maintenance',
  DEPOT : 'depot',

}

function url(s: string) {
  if (s==''){
    return ['/api/log', ''].join('/')
  }
  return ['/api/log', s.replace('/', ''), ''].join('/')
}

export async function listOrderLog(order_id:number){
  const res = await api({
    url: url('get_log_by_type'),
    method: 'post',
    data: {
      repair_order_id : order_id,
      log_type : log_type.MAINTENANCE
    }
  })
  return res.data as EventLogInterface[]
}

export async function filter_log(
  log_type:string,  // section
  action_type:string, //action
  category_type:string, //sub_section
  stock_code:string, 
  serial_no:string,
  job_order:string,
  dateFrom:string,
  dateTo:string,
  currentPage: number
  ){
  const res = await api({
    url: url('filter_log'),
    method: 'post',
    data:{
      log_type,
      action_type,
      category_type,
      stock_code,
      serial_no,
      job_order,
      dateFrom,
      dateTo,
      currentPage
    },
    params:{
      page:currentPage,
    }
  })
  return res.data as EventLogInterface[] & MaxPage
}

export async function getOrderOpts(){
  const res = await api({
    url: url('get_order_opts'),
    method: 'post'
  })
  return res.data as LogOpts
}

export async function getSNbySC(stock_code:string){
  const res = await api({
    url: url('get_serial_no_by_stock_code'),
    method: 'post',
    data:{stock_code}
  })
  return res.data as LogOpts
}

export async function getDepotLogByPart(stock_code:string, serial_no:string) {
  const res = await api({
    url: url('get_depot_log_by_part'),
    method: 'post',
    data:{
      stock_code,
      serial_no
    }
  })
  return res.data as EventLogInterface[]
}

export async function getDepotLogByLocation(car:string) {
  const res = await api({
    url: url('get_depot_log_by_car'),
    method: 'post',
    data:{
      car
    }
  })
  return res.data as EventLogInterface[]
}